package com.bf.electroplating.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bf.electroplating.pojo.dto.IdDTO;
import com.bf.electroplating.pojo.dto.renovate.ControlProblemRenovateAddDTO;
import com.bf.electroplating.pojo.dto.renovate.ControlProblemRenovateQueryDTO;
import com.bf.electroplating.pojo.dto.renovate.ControlProblemRenovateUpdateDTO;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.renovate.ControlProblemRenovateVO;
import com.bf.electroplating.service.ControlProblemRenovateService;
import com.bf.electroplating.util.easyexcel.excel.problem.ProblemRenovateExcel;
import com.bf.electroplating.util.easyexcel.listener.ProblemRenovateExcelListener;
import com.bf.electroplating.util.easyexcel.utils.ProblemImageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 整改问题管理
 *
 * @author 15451
 */
@Tag(name = "整改问题管理")
@RestController
@RequestMapping("/control/renovate")
public class ControlProblemRenovateController {

    @Resource
    private ControlProblemRenovateService renovateService;
    @Resource
    private ProblemRenovateExcelListener problemRenovateExcelListener;
    @Resource
    private ProblemImageUtils problemImageUtils;

    @Operation(summary = "新增整改问题")
    @PostMapping("/add")
    public ResultVO<String> add(@RequestBody ControlProblemRenovateAddDTO dto) {
        if (CollectionUtils.isEmpty(dto.getPhotoList())) {
            return ResultVO.fail("请上传图片");
        }
        renovateService.save(dto);
        return ResultVO.ok(null, "保存成功");
    }

    @Operation(summary = "修改整改问题")
    @PostMapping("/update")
    public ResultVO<String> update(@RequestBody ControlProblemRenovateUpdateDTO dto) {
        renovateService.update(dto);
        return ResultVO.ok(null, "编辑成功");
    }

    @Operation(summary = "删除整改问题")
    @PostMapping("/delete")
    public ResultVO<String> delete(@RequestBody IdDTO id) {
        renovateService.delete(id.getId());
        return ResultVO.ok(null, "删除成功");
    }

    @Operation(summary = "批量删除整改问题")
    @PostMapping("/batchDelete")
    public ResultVO<String> batchDelete(@RequestBody List<String> ids) {
        renovateService.batchDelete(ids);
        return ResultVO.ok(null, "批量删除成功");
    }

    @Operation(summary = "整改问题详情")
    @PostMapping("/detail")
    public ResultVO<ControlProblemRenovateVO> detail(@RequestBody IdDTO id) {
        return ResultVO.ok(renovateService.detail(id.getId()));
    }

    @Operation(summary = "整改问题分页列表")
    @PostMapping("/page")
    public ResultVO<Page<ControlProblemRenovateVO>> page(@RequestBody ControlProblemRenovateQueryDTO dto) {
        return ResultVO.ok(renovateService.page(dto));
    }

    @PostMapping(value = "/importData")
    @ResponseBody
    @Operation(summary = "导入")
    public ResultVO<Void> importData(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, List<String>> listMap = problemImageUtils.imageProcess(file);
            problemRenovateExcelListener.setImageMap(listMap);
            EasyExcel.read(file.getInputStream(), ProblemRenovateExcel.class, problemRenovateExcelListener).sheet()
                    .headRowNumber(2) // 从第1行开始读取数据，0开始
                    .doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ResultVO.ok("导入成功");
    }

}